package com.food.restaurantpro.adapters;

import java.util.ArrayList;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.imageloader.FileCache;
import com.food.restaurantpro.imageloader.ImageLoader;
import com.food.restaurantpro.objects.Promotion;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PromotionGalleryAdapter extends BaseAdapter {
	private Context mContext;
	private static LayoutInflater inflater = null;
	private ArrayList<Promotion> mListPromotion;

//	private ImageLoader imageLoader;
	private FileCache m_FileCache;

	public PromotionGalleryAdapter(Context context, ArrayList<Promotion> mList) {
		this.mContext = context;
		this.mListPromotion = mList;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		imageLoader = new ImageLoader(mContext, R.drawable.image0, 1);
		m_FileCache = new FileCache(RestaurantApp.CURRENT_CONTEXT);
	}

	@Override
	public int getCount() {
		return mListPromotion.size();
	}

	@Override
	public Object getItem(int position) {
		return mListPromotion.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static class ViewHolder {
		public TextView tvTitle;
		public TextView tvContent;
		public TextView tvDiscount;
		public ImageView image;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		final ViewHolder holder;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.gl_item, null);
			holder = new ViewHolder();
			holder.tvTitle = (TextView) vi.findViewById(R.id.gl_tv_title);
			holder.tvContent = (TextView) vi.findViewById(R.id.gl_tv_content);
			holder.tvDiscount = (TextView) vi.findViewById(R.id.gl_tv_discount);
			holder.image = (ImageView) vi.findViewById(R.id.gl_image_item);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}

		holder.tvTitle.setText(mListPromotion.get(position).getPromotionName());
		holder.tvContent.setText(mListPromotion.get(position)
				.getPromotionSologan());
		holder.tvDiscount.setText(mListPromotion.get(position)
				.getPromotionDiscount());
//		imageLoader.displayImage(mListPromotion.get(position).getPromotionImageSlide(), holder.image);
		holder.image.setImageBitmap(m_FileCache.getBitmapByUrl(mListPromotion.get(position).getPromotionImageSlide()));
		holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
		return vi;
	}

	public void clearMemoryCache() {
		if (mListPromotion != null) {
			mListPromotion.clear();
			mListPromotion = null;
		}
		inflater = null;
//		imageLoader.clearMemoryCache();
		m_FileCache = null;
	}
}
